package com.lemon.smartsafetynew.ui.adapter.me;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.user.MyHeaderImgEvent;
import com.lemon.smartsafety.ui.adapter.profile.ProfileHeadItemView;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.ImgLoadUtil;
import com.lemon.smartsafety.utils.app.UserTypeHelper;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.EventTagConstants;
import com.lemon.smartsafetynew.entity.event.common.ParamsEvent;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.me.MePanelLabel;
import com.lemon.smartsafetynew.entity.me.MeRcvItemBase;
import com.lemon.smartsafetynew.util.user.UserTypeUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/7/26.
 */

public class MeInfoAdapterItem implements AdapterItem<MeRcvItemBase<User>> {


    private Button settingBtn;
    private CircleImageView userIconImageView;
    private TextView userNameTv;
    private TextView userJobTv;
    private TextView userCompanyTv;
    private ImageView userLevelImageView;
    private TextView userLevelTv;
    private ProgressBar levelProgressBar;
    private TextView userAddressTv;

    @Override
    public int getLayoutResId() {
        return R.layout.item_user_info;
    }

    @Override
    public void bindViews(View root) {

        settingBtn = (Button) root.findViewById(R.id.setting_btn);
        userIconImageView = (CircleImageView) root.findViewById(R.id.user_icon_imageView);
        userNameTv = (TextView) root.findViewById(R.id.user_name_tv);
        userJobTv = (TextView) root.findViewById(R.id.user_job_tv);
        userCompanyTv = (TextView) root.findViewById(R.id.user_company_tv);
        userLevelImageView = (ImageView) root.findViewById(R.id.user_level_imageView);
        userLevelTv = (TextView) root.findViewById(R.id.user_level_tv);
        levelProgressBar = (ProgressBar) root.findViewById(R.id.level_progress_bar);
        userAddressTv = (TextView) root.findViewById(R.id.user_address_tv);

    }

    @Override
    public void setViews() {

        userIconImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                MyHeaderImgEvent event = new MyHeaderImgEvent();
//                event.setTag(ProfileHeadItemView.class.getName());
//                AppService.getBus().post(event);

                AppService.getBus().post(new ViewEvent(EventTagConstants.ME_CHANGE_USER_ICON_TAG));
            }
        });

        settingBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //TODO
            }
        });

    }

    @Override
    public void handleData(MeRcvItemBase<User> itemBase, int position) {

        User user = itemBase.getData();
        userNameTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getUserName()));

        //userJobTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getJob()));
        if(user.getUserType() != null) {
            userJobTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getUserTypeName()));
        }

        if(UserTypeUtil.isGovType(user.getUserType())) { //政府人员
            userCompanyTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getGovName()));
        } else {                                        //企业人员
            userCompanyTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getCompanyName()));
        }

        userAddressTv.setText("地区："
                + com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getProvinceName())
                + com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(user.getCityName()));

        if (!CommonUtil.strIsEmpty(user.getPhotoURL())) {
            try {
                ImgLoadUtil.setImgPath(user.getPhotoURL(), userIconImageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            userIconImageView.setBackgroundResource(R.mipmap.img_placeholder);
        }

    }

}
